{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "47ffae3d-c885-4c9f-8fd5-0ea4eb6a98e6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e90dc668-05a9-4a86-849d-921a732b57a6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def subtract_aragonite(atom_perc_dict):\n",
    "    remaining_perc_dict = {}\n",
    "    perc_Ca = atom_perc_dict['Ca']\n",
    "    total_perc = 0\n",
    "    for key, val in atom_perc_dict.items():\n",
    "        if key == 'Ca':\n",
    "            continue\n",
    "        if key == 'C':\n",
    "            remaining_perc_dict[key] = val - perc_Ca\n",
    "        elif key == 'O':\n",
    "            remaining_perc_dict[key] = val - (3* perc_Ca)\n",
    "        else:\n",
    "            remaining_perc_dict[key] = val\n",
    "        \n",
    "        total_perc += remaining_perc_dict[key]\n",
    "    for key, val in remaining_perc_dict.items():\n",
    "        remaining_perc_dict[key] = val * (100/total_perc)\n",
    "    return remaining_perc_dict\n",
    "\n",
    "def ratio_to_Fe(atom_perc_dict):\n",
    "    ratios = {}\n",
    "    try:\n",
    "        Fe_perc = atom_perc_dict['Fe']\n",
    "    except:\n",
    "        return 'No Fe'\n",
    "    for key, val in atom_perc_dict.items():\n",
    "        if key == 'Fe':\n",
    "            continue\n",
    "        ratios[key] = val/Fe_perc\n",
    "    return ratios\n",
    "\n",
    "def subtract_alumina(ratios_dict):\n",
    "    remaining_ratios_dict = {}\n",
    "    try:\n",
    "        Al = ratios_dict['Al']\n",
    "    except:\n",
    "        return ratios_dict\n",
    "    for key, val in ratios_dict.items():\n",
    "        if key == 'Al':\n",
    "            continue\n",
    "        if key == 'O':\n",
    "            remaining_ratios_dict[key] = val-3*Al/2\n",
    "        remaining_ratios_dict[key] = val\n",
    "    return remaining_ratios_dict\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9c4ed3ac-ddb8-4434-b261-0602c0180e34",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "EDS_values = {}\n",
    "EDS_values['17_1'] = {'C':32.57, 'O':49.16, 'Al':5.01, 'Si':0.2, 'Ca':12.49, 'Fe':0.57}\n",
    "EDS_values['16_1'] = {'C': 21.1, 'O':60.74, 'Ca':13.42, 'Fe':4.74}\n",
    "EDS_values['6_1'] = {'C': 11.28, 'O':60.61, 'Ca':0.84, 'Ti':1.02, 'Fe':26.25}\n",
    "EDS_values['6_2'] = {'C': 19.47, 'O':59.35, 'K':0.33, 'Al':3.21, 'Si':2.46, 'P':0.26, 'Cl':0.13, 'Ca':4.58, 'Ti':11, 'Fe':1.42}\n",
    "EDS_values['6_5'] = {'C': 44.26, 'N':12.93, 'O':32.22, 'Al':0.42, 'Si':3.86, 'Cl':3.86, 'Ca':5.79, 'Fe':0.38}\n",
    "EDS_values['5_1'] = {'C': 15.07, 'O':60.56, 'Al':0.31, 'Ca':7.93, 'Ti':2.21, 'Fe':18.16}\n",
    "EDS_values['5_2'] = {'C':36.07, 'O':46.84, 'Al':0.34, 'Si':0.26, 'Cl':0.11, 'Ca':11.7, 'Ti':4.47, 'Fe':0.21}\n",
    "EDS_values['4_1'] = {'C':17.76, 'O':60.74, 'Al':1.71, 'Si':1.06, 'P':0.23, 'Ca':4.82, 'Fe':13.69}\n",
    "EDS_values['4_3'] = {'C':14.64, 'O':65.64, 'Al':0.63, 'Si':0.51, 'P':0.13, 'Ca':12.14, 'Ti':1.23, 'Fe':5.08}\n",
    "EDS_values['2_1'] = {'C':15.9, 'O':64.08, 'Al':2.07, 'Si':0.73, 'Ca':10.27, 'Fe':6.94}\n",
    "EDS_values['2_2'] = {'C':19.6, 'O':61.59, 'Ca':18.8}\n",
    "EDS_values['2_3'] = {'C':13.72, 'O':62.35, 'Mg':0.41, 'Al':2.03, 'Si':1.83, 'Ca':1.82, 'Fe':17.84}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e079107c-df69-4969-9b33-42b7a1bc9cb6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "EDS17_1 percentages without aragonite: {'C': 53.475366178428764, 'O': 31.13182423435419, 'Al': 13.342210386151798, 'Si': 0.5326231691078562, 'Fe': 1.51797603195739}\n",
      "EDS17_1 relative abundances with Fe: {'C': 35.2280701754386, 'O': 20.50877192982456, 'Al': 8.789473684210527, 'Si': 0.3508771929824562}\n",
      "EDS17_1 relative abundances with Fe (subtracted Alumina): {'C': 35.2280701754386, 'O': 20.50877192982456, 'Si': 0.3508771929824562}\n",
      "\n",
      "EDS16_1 percentages without aragonite: {'C': 23.343465045592705, 'O': 62.24924012158055, 'Fe': 14.407294832826745}\n",
      "EDS16_1 relative abundances with Fe: {'C': 1.6202531645569624, 'O': 4.3206751054852335}\n",
      "EDS16_1 relative abundances with Fe (subtracted Alumina): {'C': 1.6202531645569624, 'O': 4.3206751054852335}\n",
      "\n",
      "EDS6_1 percentages without aragonite: {'C': 10.897703549060543, 'O': 60.636743215031316, 'Ti': 1.0647181628392486, 'Fe': 27.400835073068897}\n",
      "EDS6_1 relative abundances with Fe: {'C': 0.39771428571428563, 'O': 2.212952380952381, 'Ti': 0.038857142857142854}\n",
      "EDS6_1 relative abundances with Fe (subtracted Alumina): {'C': 0.39771428571428563, 'O': 2.212952380952381, 'Ti': 0.038857142857142854}\n",
      "\n",
      "EDS6_2 percentages without aragonite: {'C': 18.77442945404111, 'O': 57.50851090656917, 'K': 0.4160887656033288, 'Al': 4.0474089017778345, 'Si': 3.1017526163157236, 'P': 0.3278275122935318, 'Cl': 0.1639137561467659, 'Ti': 13.86962552011096, 'Fe': 1.7904425671415964}\n",
      "EDS6_2 relative abundances with Fe: {'C': 10.485915492957748, 'O': 32.11971830985916, 'K': 0.23239436619718312, 'Al': 2.2605633802816905, 'Si': 1.7323943661971832, 'P': 0.1830985915492958, 'Cl': 0.0915492957746479, 'Ti': 7.746478873239437}\n",
      "EDS6_2 relative abundances with Fe (subtracted Alumina): {'C': 10.485915492957748, 'O': 32.11971830985916, 'K': 0.23239436619718312, 'Si': 1.7323943661971832, 'P': 0.1830985915492958, 'Cl': 0.0915492957746479, 'Ti': 7.746478873239437}\n",
      "\n",
      "EDS6_5 percentages without aragonite: {'C': 51.45111675805805, 'N': 17.29303196469172, 'O': 19.860906780794434, 'Al': 0.5617226160224689, 'Si': 5.1624983282065005, 'Cl': 5.1624983282065005, 'Fe': 0.508225224020329}\n",
      "EDS6_5 relative abundances with Fe: {'C': 101.23684210526316, 'N': 34.026315789473685, 'O': 39.07894736842105, 'Al': 1.1052631578947367, 'Si': 10.157894736842106, 'Cl': 10.157894736842106}\n",
      "EDS6_5 relative abundances with Fe (subtracted Alumina): {'C': 101.23684210526316, 'N': 34.026315789473685, 'O': 39.07894736842105, 'Si': 10.157894736842106, 'Cl': 10.157894736842106}\n",
      "\n",
      "EDS5_1 percentages without aragonite: {'C': 11.054342777519741, 'O': 56.928317076946904, 'Al': 0.4799504567270475, 'Ti': 3.4215822882799194, 'Fe': 28.115807400526396}\n",
      "EDS5_1 relative abundances with Fe: {'C': 0.39317180616740094, 'O': 2.0247797356828197, 'Al': 0.017070484581497798, 'Ti': 0.12169603524229075}\n",
      "EDS5_1 relative abundances with Fe (subtracted Alumina): {'C': 0.39317180616740094, 'O': 2.0247797356828197, 'Ti': 0.12169603524229075}\n",
      "\n",
      "EDS5_2 percentages without aragonite: {'C': 58.72289156626505, 'O': 28.28915662650604, 'Al': 0.8192771084337348, 'Si': 0.6265060240963853, 'Cl': 0.26506024096385533, 'Ti': 10.771084337349395, 'Fe': 0.506024096385542}\n",
      "EDS5_2 relative abundances with Fe: {'C': 116.04761904761905, 'O': 55.90476190476195, 'Al': 1.6190476190476193, 'Si': 1.2380952380952381, 'Cl': 0.5238095238095238, 'Ti': 21.285714285714285}\n",
      "EDS5_2 relative abundances with Fe (subtracted Alumina): {'C': 116.04761904761905, 'O': 55.90476190476195, 'Si': 1.2380952380952381, 'Cl': 0.5238095238095238, 'Ti': 21.285714285714285}\n",
      "\n",
      "EDS4_1 percentages without aragonite: {'C': 17.046502437096564, 'O': 60.96693452773021, 'Al': 2.2526676327229613, 'Si': 1.396390462389672, 'P': 0.30299038334870243, 'Fe': 18.034514556711894}\n",
      "EDS4_1 relative abundances with Fe: {'C': 0.9452154857560265, 'O': 3.3805697589481376, 'Al': 0.12490869247626005, 'Si': 0.07742878013148284, 'P': 0.01680058436815194}\n",
      "EDS4_1 relative abundances with Fe (subtracted Alumina): {'C': 0.9452154857560265, 'O': 3.3805697589481376, 'Si': 0.07742878013148284, 'P': 0.01680058436815194}\n",
      "\n",
      "EDS4_3 percentages without aragonite: {'C': 6.361323155216285, 'O': 74.35114503816794, 'Al': 1.6030534351145038, 'Si': 1.2977099236641223, 'P': 0.33078880407124683, 'Ti': 3.1297709923664123, 'Fe': 12.926208651399492}\n",
      "EDS4_3 relative abundances with Fe: {'C': 0.49212598425196846, 'O': 5.751968503937008, 'Al': 0.12401574803149606, 'Si': 0.10039370078740158, 'P': 0.025590551181102362, 'Ti': 0.2421259842519685}\n",
      "EDS4_3 relative abundances with Fe (subtracted Alumina): {'C': 0.49212598425196846, 'O': 5.751968503937008, 'Si': 0.10039370078740158, 'P': 0.025590551181102362, 'Ti': 0.2421259842519685}\n",
      "\n",
      "EDS2_1 percentages without aragonite: {'C': 11.574835526315793, 'O': 68.40049342105263, 'Al': 4.255756578947369, 'Si': 1.5008223684210529, 'Fe': 14.268092105263161}\n",
      "EDS2_1 relative abundances with Fe: {'C': 0.8112391930835735, 'O': 4.793948126801151, 'Al': 0.2982708933717579, 'Si': 0.10518731988472622}\n",
      "EDS2_1 relative abundances with Fe (subtracted Alumina): {'C': 0.8112391930835735, 'O': 4.793948126801151, 'Si': 0.10518731988472622}\n",
      "\n",
      "EDS2_2 percentages without aragonite: {'C': 13.355592654424056, 'O': 86.64440734557594}\n",
      "EDS2_2 relative abundances with Fe: No Fe\n",
      "EDS2_2 relative abundances with Fe (subtracted Alumina): No Fe\n",
      "\n",
      "EDS2_3 percentages without aragonite: {'C': 13.09130913091309, 'O': 62.58525852585257, 'Mg': 0.45104510451045093, 'Al': 2.2332233223322326, 'Si': 2.013201320132013, 'Fe': 19.625962596259622}\n",
      "EDS2_3 relative abundances with Fe: {'C': 0.6670403587443946, 'O': 3.18890134529148, 'Mg': 0.022982062780269055, 'Al': 0.11378923766816143, 'Si': 0.10257847533632287}\n",
      "EDS2_3 relative abundances with Fe (subtracted Alumina): {'C': 0.6670403587443946, 'O': 3.18890134529148, 'Mg': 0.022982062780269055, 'Si': 0.10257847533632287}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for label, percentages in EDS_values.items():\n",
    "    print(f'EDS{label} percentages without aragonite: {subtract_aragonite(percentages)}')\n",
    "    no_Fe = ratio_to_Fe(subtract_aragonite(percentages))\n",
    "    print(f'EDS{label} relative abundances with Fe: {no_Fe}')\n",
    "    print(f'EDS{label} relative abundances with Fe (subtracted Alumina): {subtract_alumina(no_Fe)}\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0b83b0b-f509-4259-aeff-5d5b4b62e5f7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
